package com.github.pulltorefreshproject;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.github.pulltorefreshproject.databinding.ActivityMainBinding;
import com.github.pulltorefreshproject.databinding.LeftMainBinding;
import com.github.refresh.PullDown;
import com.github.refresh.PullDownListener;
import com.github.refresh.PullRefreshLayout;
import com.github.refresh.RefreshLayout;
import com.github.refresh.RefreshLayoutHelper;
import com.github.refresh.RefreshListener;

public class MainActivity extends AppCompatActivity {

    private com.github.pulltorefreshproject.databinding.ActivityMainBinding bind;
    private com.github.pulltorefreshproject.databinding.LeftMainBinding left;

    long time = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ToastUtils.init(getApplication());
        bind = ActivityMainBinding.inflate(getLayoutInflater());
        left = bind.left;
        setContentView(bind.getRoot());
        initData();

        TextView tvOffset = findViewById(R.id.tvOffset);
        PullRefreshLayout rlLayout = findViewById(R.id.rlLayout);
        rlLayout.setRefreshListener(new RefreshListener() {
            @Override
            public void onRefresh(RefreshLayout layout) {
                rlLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.refreshComplete(bind.cbRefreshSuccess.isChecked());
                    }
                }, time * 1000);
            }

            @Override
            public void onOpenPage(RefreshLayout layout) {
                ToastUtils.showToast("打开页面");
            }
        });
        View btAutoRefresh = findViewById(R.id.btAutoRefresh);
        btAutoRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rlLayout.autoRefresh();
            }
        });
        bind.etRefreshTime.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s)) {
                    try {
                        time = Integer.parseInt(s.toString());
                    } catch (Exception e) {
                        time = 3;
                    }
                } else {
                    time = 3;
                }
            }
        });

        bind.dlLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {
                setHelper();
            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });

        rlLayout.setListener(new PullDownListener() {
            @Override
            public void pullDownDistance(RefreshLayout layout, PullDown pullDown) {
                tvOffset.setText("下拉距离：" + pullDown.topDistance
                        + " ,是否下拉：" + pullDown.isPullDown
                        + " ,偏移量：" + pullDown.offsetY
                        + " ,是否是触摸：" + pullDown.isTouch
                        + " ,header高度：" + pullDown.headerViewHeight
                        + " ,状态：" + pullDown.status
                );
            }
        });

        TextView tvTest = findViewById(R.id.tvTest);
        tvTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int top = tvTest.getTop();
                Log.i("=====", "====top=" + top);
                ToastUtils.showToast("点击内容");
            }
        });
        tvTest.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ToastUtils.showToast("=长按内容=");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    WebView.setDataDirectorySuffix("");
                }
                return true;
            }
        });
        TextView tvHead = findViewById(R.id.tvHead);
        if (tvHead != null) {
            tvHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtils.showToast("点击刷新头");
                }
            });
            tvHead.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    ToastUtils.showToast("=长按刷新头=");
                    return true;
                }
            });
        }

    }

    private void initData() {
        ListAdapter adapter = new ListAdapter();
        bind.rvList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        bind.rvList.setAdapter(adapter);


        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter();
        bind.viewPager.setAdapter(viewPagerAdapter);
    }

    private void setHelper() {
        RefreshLayoutHelper helper = bind.rlLayout.getHelper();
        helper.setReleaseRefresh(left.cbReleaseRefresh.isChecked());

        String resistance = left.etPullDownResistance.getText().toString();
        if (TextUtils.isEmpty(resistance)) {
            resistance = "0";
        }
        helper.setPullDownResistance(Float.parseFloat(resistance));

        String pullDownHeaderHeightRefreshRatio = left.etPullDownHeaderHeightRefreshRatio.getText().toString();
        if (TextUtils.isEmpty(pullDownHeaderHeightRefreshRatio)) {
            pullDownHeaderHeightRefreshRatio = "0";
        }
        helper.setPullDownHeaderHeightRefreshRatio(Float.parseFloat(pullDownHeaderHeightRefreshRatio));

        String pullDownHeaderHeightRefresh = left.etPullDownHeaderHeightRefresh.getText().toString();
        if (TextUtils.isEmpty(pullDownHeaderHeightRefresh)) {
            pullDownHeaderHeightRefresh = "0";
        }
        helper.setPullDownHeaderHeightRefresh(dp2px(Float.parseFloat(pullDownHeaderHeightRefresh)));

        String PullDownHeaderHeightOpenPageRatio = left.etPullDownHeaderHeightOpenPageRatio.getText().toString();
        if (TextUtils.isEmpty(PullDownHeaderHeightOpenPageRatio)) {
            PullDownHeaderHeightOpenPageRatio = "0";
        }
        helper.setPullDownHeaderHeightOpenPageRatio(Float.parseFloat(PullDownHeaderHeightOpenPageRatio));

        String setPullDownHeaderHeightOpenPage = left.etPullDownHeaderHeightOpenPage.getText().toString();
        if (TextUtils.isEmpty(setPullDownHeaderHeightOpenPage)) {
            setPullDownHeaderHeightOpenPage = "0";
        }
        helper.setPullDownHeaderHeightOpenPage(dp2px(Integer.parseInt(setPullDownHeaderHeightOpenPage)));

        String setDurationCloseForOpenPage = left.etDurationCloseForOpenPage.getText().toString();
        if (TextUtils.isEmpty(setDurationCloseForOpenPage)) {
            setDurationCloseForOpenPage = "0";
        }
        helper.setDurationCloseForOpenPage(Long.parseLong(setDurationCloseForOpenPage));

        String refreshResultDurationTime = left.etRefreshResultDurationTime.getText().toString();
        if (TextUtils.isEmpty(refreshResultDurationTime)) {
            refreshResultDurationTime = "0";
        }
        helper.setRefreshResultDurationTime(Long.parseLong(refreshResultDurationTime));

        String setDurationClose = left.etDurationClose.getText().toString();
        if (TextUtils.isEmpty(setDurationClose)) {
            setDurationClose = "0";
        }
        helper.setDurationClose(Long.parseLong(setDurationClose));

        String setDurationCloseHeader = left.etDurationCloseHeader.getText().toString();
        if (TextUtils.isEmpty(setDurationCloseHeader)) {
            setDurationCloseHeader = "0";
        }
        helper.setDurationCloseHeader(Long.parseLong(setDurationCloseHeader));

        helper.setRefreshKeepHeader(left.cbRefreshKeepHeader.isChecked());
        helper.setRefreshShowSuccess(left.cbRefreshShowSuccess.isChecked());
        helper.setRefreshShowError(left.cbRefreshShowError.isChecked());
        helper.setNeedHorizontalScroll(left.cbNeedHorizontalScroll.isChecked());

        bind.rlLayout.setShowTimeTips(left.cbShowTimeTips.isChecked());

        String setHorizontalScrollXYRatio = left.etHorizontalScrollXYRatio.getText().toString();
        if (TextUtils.isEmpty(setHorizontalScrollXYRatio)) {
            setHorizontalScrollXYRatio = "0";
        }
        helper.setHorizontalScrollXYRatio(Float.parseFloat(setHorizontalScrollXYRatio));


        bind.rlLayout.setRefreshingText(left.etRefreshingText.getText().toString());
        bind.rlLayout.setPullDownRefreshText(left.etPullDownRefreshText.getText().toString());
        bind.rlLayout.setReleaseRefreshText(left.etReleaseRefreshText.getText().toString());
        bind.rlLayout.setReleaseOpenPageText(left.etReleaseOpenPageText.getText().toString());
        bind.rlLayout.setRefreshSuccessText(left.etRefreshSuccessText.getText().toString());
        bind.rlLayout.setRefreshErrorText(left.etRefreshErrorText.getText().toString());


    }

    private int dp2px(float value) {
        return (int) (getResources().getDisplayMetrics().density * value);
    }
}